import 'package:common/common.dart';
import 'package:get/get.dart';

import 'report_state.dart';

/// @description:
/// @author
/// @date: 2024-09-23 16:13:32
class ReportLogic extends GetxController {
  final state = ReportState();

  void doPostFeekBack() async {
    logs('提交意见反馈   state.content = ${state.content}  state.phone = ${state.phone} state.imageIds = ${state.imageIds} ');

    if (state.content.isEmpty) {
      Toast.showText('请输入反馈内容');
      return;
    }
    if (state.phone.isEmpty) {
      Toast.showText('请输入手机号码');
      return;
    }
    if (state.phone.length != 11 || !state.phone.startsWith('1')) {
      Toast.showText('请输入正确的手机号码');
      return;
    }

    if (state.filePaths.isEmpty) {
      onSubmit();
    } else {
      onUploadImages(callback: onSubmit);
    }
  }

  /// 提交
  void onSubmit() async {
    Result result = await HttpProvider.post('/manager/suggestion',
        params: {
          'content': state.content,
          'phone_number': state.phone,
          'images': state.imageIds,
        },
        explain: '创建意见反馈');
    logs(result.toString());
    if (result.succ) {
      Toast.showText(
        '反馈内容已提交,我们将会安排工作人员与您联系',
        onDismiss: () {
          Get.back();
        },
      );
    } else {
      Toast.showText('内容提交失败,请重试');
    }
  }

  /// 上传图片
  void onUploadImages({required Function callback}) async {
    UploadImage.onUploadImages(
      filePaths: state.filePaths,
      type: UploadImageType.suggestion,
      callBack: (imageurls, ids) {
        state.imageIds = ids;
        callback();
      },
    );
  }

  @override
  void dispose() {
    state.focusNode.dispose();
    state.controller.dispose();
    super.dispose();
  }
}
